# @File    : kill_ota.py
# @Project : Airtest_JBL_IOS
from poco.exceptions import PocoNoSuchNodeException
from exception.custom_exception import ElectricException
from iOS.cancel_ota import CancelOta

from airtest.core.api import *
import logging
from common.logUtil import info_i, warning_i
logging.getLogger("airtest").setLevel(logging.CRITICAL)


class KillOta(CancelOta):

    h_version = CancelOta.location.read_yaml()['h_version']
    l_version = CancelOta.location.read_yaml()['l_version']
    s_h_version = CancelOta.location.read_yaml()['s_h_version']
    s_l_version = CancelOta.location.read_yaml()['s_l_version']
    test_device = CancelOta.location.read_yaml()['device']

    def kill_ota(self, num):
        self.check_is_local_ota()
        version = self.check_version()
        i = 1
        while i < int(num) + 1:
            try:
                if version == self.l_version:
                    if i == 1:
                        self.select_version(version, self.test_device)
                    self.start_ota(version)
                    self.check_upgrade(i)
                    if self.test_device == "耳机":
                        version = self.poco(nameMatches=f"{self.earbud}").get_name()[3:]
                    else:
                        version = self.poco(nameMatches=f"{self.case}").get_name()[4:]
                    if version == self.h_version:
                        i -= 1
                elif version == self.h_version:
                    self.is_height(i, version)
                    i -= 1
                else:
                    i -= 1
                i += 1
            except ElectricException:
                self.fail_num += 1
                self.fail_list.append(i)
                warning_i(f"第 {i} 次升级时耳机电量过低，无法升级!")
                break
            except TargetNotFoundError:
                self.fail_num += 1
                self.fail_list.append(i)
                warning_i("出现异常了，请手动跟换安装包或者出现运行程序")
                break
            except PocoNoSuchNodeException:
                break
        info_i(f"中断升级测试完成, 共压测 {i - 1} 次, 其中 PASS {self.pass_num} 次, FAIL {self.fail_num} 次, FAIL的轮数为{self.fail_list}")
        self.fail_list.clear()

    def check_upgrade(self, i):
        while True:
            if self.poco("重试").exists():
                self.poco("重试").click()
                self.poco("更新").click()
            if self.poco(nameMatches=f"{self.schedule}").exists():
                result = self.is_install(self.poco(nameMatches=f"{self.process}").get_name())
                if result:
                    while True:
                        if self.poco("更新成功").exists():
                            self.poco("完成").click()
                            break
                    break
                else:
                    sleep(4)
                    stop_app(f"{self.location.read_yaml()['app_ios']}")
                    sleep(1)
                    start_app(f"{self.location.read_yaml()['app_ios']}")
                    other = self.poco("Others").wait(15)
                    if other.exists():
                        other.click()
                        swipe((self.width * 0.5, self.height * 0.88), (self.width * 0.5, self.height * 0.6))
                        self.poco("支持").click()
                        if self.test_device == "耳机":
                            version = self.poco(nameMatches=f"{self.earbud}").get_name()[3:]
                        else:
                            version = self.poco(nameMatches=f"{self.case}").get_name()[4:]
                        self.check_result(i, version)
                    break

    def check_result(self, i, version):
        if version == self.l_version:
            info_i(f"第 {i} 次中断升级后的版本为: {version}")
            sleep(1)
            info_i(f"第 {i} 次测试结果为PASS")
            self.pass_num += 1
        else:
            warning_i(f"第 {i} 次中断升级后的版本为: {version}")
            sleep(1)
            warning_i(f"第 {i} 次测试结果为FAIL")
            self.fail_num += 1
            self.fail_list.append(i)
